var app = getApp();
Page({

    /** 
     * 页面的初始数据 
     */
    data: {
        action: null,
        inputValue: null,
        empty: false
    },

    /** 
    * 生命周期函数--监听页面加载 
    */
    onLoad(options) {
        this.setData({ "action": options.action })
        if (options.group_id) this.setData({ "group_id": options.group_id })
    },

    search_change(event) {
        this.setData({
            inputValue: event.detail.value
        })
    },

    commit() {
        var input_data = this.data.inputValue;
        if (!input_data) {
            //搜索内容为空
            wx.showToast({
                icon: 'error',
                title: '请输入搜索内容'
            })
            return false;
        }
        var reg = /^\d+$/;
        if (!reg.test(input_data)) {
            wx.showToast({
                icon: 'error',
                title: '请勿输入非数字'
            })
            return false;
        }
        if (this.data.action == 'join') this.searchGroup();
        if (this.data.action == 'invite') this.searchMember();
    },

    searchGroup: function () {
        var that = this;
        wx.request({
            url: app.buildUrl("/group/search"),
            header: app.getRequestHeader(),
            data: {
                group_id: that.data.inputValue,
            },
            success: function (res) {
                var resp = res.data;
                var group_info = resp.data.group_data;
                if (resp.code != 200) {
                    app.alert({
                        "content": resp.msg
                    });
                    return;
                }
                if (JSON.stringify(group_info) == "{}") {
                    that.setData({
                        empty: true
                    })
                    return;
                }
                setTimeout(function () {
                    that.setData({
                        empty: false
                    })
                    wx.navigateTo({
                        url: `/pages/group/info?gid=${that.data.inputValue}&isJoin=${true}`
                    });
                }, 200);
            }
        });
    },

    searchMember: function() {
        var that = this;
        wx.request({
            url: app.buildUrl("/user/search"),
            header: app.getRequestHeader(),
            data: {
                user_id: that.data.inputValue,
            },
            success: function (res) {
                var resp = res.data;
                var user_info = resp.data.user_info;
                if (resp.code != 200) {
                    app.alert({
                        "content": resp.msg
                    });
                    return;
                }
                if (JSON.stringify(user_info) == "{}") {
                    that.setData({
                        empty: true
                    })
                    return;
                }
                setTimeout(function () {
                    that.setData({
                        empty: false
                    })
                    wx.navigateTo({
                        url: `/pages/userinfo/userinfo?uid=${that.data.inputValue}&action=invite&group_id=${that.data.group_id}`
                    });
                }, 200);
            }
        });
    },

    /** 
     * 生命周期函数--监听页面初次渲染完成 
     */
    onReady() {

    },

    /** 
     * 生命周期函数--监听页面显示 
     */
    onShow() {

    },

    /** 
     * 生命周期函数--监听页面隐藏 
     */
    onHide() {

    },

    /** 
     * 生命周期函数--监听页面卸载 
     */
    onUnload() {

    },

    /** 
     * 页面相关事件处理函数--监听用户下拉动作 
     */
    onPullDownRefresh() {

    },

    /** 
     * 页面上拉触底事件的处理函数 
     */
    onReachBottom() {

    },

    /** 
     * 用户点击右上角分享 
     */
    onShareAppMessage() {

    }
})